package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.admin.api.entity.SysUser;
import com.iwomy.secureplat.admin.api.feign.RemoteUserService;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.core.util.RetOps;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.AlarmFaultLoggingDTO;
import com.iwomy.secureplat.platform.dto.AlarmLoggingDTO;
import com.iwomy.secureplat.platform.entity.AlarmFaultLoggingEntity;
import com.iwomy.secureplat.platform.entity.AlarmLoggingEntity;
import com.iwomy.secureplat.platform.service.AlarmFaultLoggingService;
import com.iwomy.secureplat.platform.service.AlarmLoggingService;
import com.iwomy.secureplat.platform.service.BasicDeviceService;
import com.iwomy.secureplat.platform.service.ScreenDataService;
import com.iwomy.secureplat.platform.vo.AlarmLoggingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/screendata")
@Tag(description = "screendata", name = "大屏数据显示")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ScreenDataController {

	private final ScreenDataService screenDataService;

	private final RemoteUserService remoteUserService;

	private final AlarmLoggingService alarmLoggingService;


	/**
	 * 查询大屏的今日报警数和今日故障数
	 *
	 * @return R
	 */
	@Operation(summary = "查询今日报警及故障", description = "查询今日报警及故障")
	@GetMapping("/getTodayAlarmAndFault")
	public R getTodayAlarmAndFault() {
		return R.ok(screenDataService.getTodayAlarmAndFault());
	}

	@Operation(summary = "大屏设备显示信息", description = "大屏设备显示信息")
	@GetMapping("/screenDevice")
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_view')")
	public R getScreenDevice() {
		return R.ok(screenDataService.getScreenDevice());
	}

	/**
	 * 未处理报警
	 *
	 * @return R
	 */
	@Operation(summary = "查询未处理报警", description = "查询未处理报警")
	@GetMapping("/unprocessedAlarm")
	public R getUnprocessedAlarm() {
		return R.ok(screenDataService.getUnprocessedAlarm());
	}


	/**
	 * 未处理故障
	 *
	 * @return R
	 */
	@Operation(summary = "查询未处理故障", description = "查询未处理故障")
	@GetMapping("/unprocessedFault")
	public R getUnprocessedFault() {
		return R.ok(screenDataService.getUnprocessedFault());
	}

	/**
	 * 大屏地图安装点
	 *
	 * @return R
	 */
	@Operation(summary = "大屏地图安装点", description = "大屏地图安装点")
	@GetMapping("/mapPoint")
	public R mapPoint() {
		return R.ok(screenDataService.mapPoint());
	}

	/**
	 * @Return R
	 */
	@Operation(summary = "每日设备状态", description = "每日设备状态")
	@GetMapping("/deviceStateDay")
	public R deviceStateDay() {
		return R.ok(screenDataService.deviceStateDay());
	}

	/**
	 * @Return R
	 */
	@Operation(summary = "每月设备状态", description = "每月设备状态")
	@GetMapping("/deviceStateMonth")
	public R deviceStateMonth() {
		return R.ok(screenDataService.deviceStateMonth());
	}

	/**
	 * @Return R
	 */
	@Operation(summary = "产品分类占比", description = "产品分类占比")
	@GetMapping("/productClassification")
	public R productClassification() {
		return R.ok(screenDataService.productClassification());
	}

	/**
	 * 首页轮播图
	 *
	 * @Return R
	 */
	@Operation(summary = "首页轮播图", description = "首页轮播图")
	@GetMapping("/productRotation")
	public R productRotation() {
		return R.ok(screenDataService.productRotation());
	}

	/**
	 * 故障原因统计
	 *
	 * @Return R
	 */
	@Operation(summary = "故障原因统计", description = "故障原因统计")
	@GetMapping("/faultCauseStatistics")
	public R faultCauseStatistics(@ParameterObject AlarmFaultLoggingEntity alarmFaultLogging) {
		return R.ok(screenDataService.faultCauseStatistics(alarmFaultLogging));
	}


	/**
	 * @Return R
	 */
	@Operation(summary = "报警确认类型", description = "报警确认类型")
	@GetMapping("/alarmAcknowledgement")
	public R alarmAcknowledgement() {
		return R.ok(screenDataService.alarmAcknowledgement());
	}


	/**
	 * @Return R
	 */
	@Operation(summary = "首页设备安装数占比", description = "首页设备安装数占比")
	@GetMapping("/deviceInstallCount")
	public R deviceInstallCount() {
		return R.ok(screenDataService.deviceInstallCount());
	}


	/**
	 * 首页报警确认类型
	 *
	 * @Return R
	 */
	@Operation(summary = "首页报警确认类型", description = "首页报警确认类型")
	@GetMapping("/homeAlarmConfirmationType")
	public R homeAlarmConfirmationType() {
		return R.ok(screenDataService.homeAlarmConfirmationType());
	}

	/**
	 * 异常统计
	 *
	 * @Return R
	 */
	@Operation(summary = "异常统计", description = "异常统计")
	@GetMapping("/abnormalStatistics")
	public R abnormalStatistics () {
		return R.ok(screenDataService.abnormalStatistics());
	}
	/**
	 * 根据用户创建时间查询报警
	 *
	 * @Return R
	 */
	@Operation(summary = "根据用户创建时间查询报警", description = "根据用户创建时间查询报警")
	@GetMapping("/alarmLogging")
	public R alarmLogging(@ParameterObject Page page ){

		SysUser sysUser = RetOps.of(remoteUserService.getUserById(SecurityUtils.getUser().getId())).getData().orElse(null);

		LambdaQueryWrapper<AlarmLoggingEntity> ge = Wrappers.lambdaQuery(AlarmLoggingEntity.class).eq(AlarmLoggingEntity::getState, 20)
				.ge(AlarmLoggingEntity::getCreateTime, sysUser.getCreateTime());
		page = alarmLoggingService.page(page, ge);
		List records = page.getRecords();
		List collect = (List)records.stream().map(x -> {
			AlarmLoggingVO alarmLoggingVO = BeanUtil.copyProperties(x, AlarmLoggingVO.class);
			alarmLoggingVO.setCreateTime2Long(alarmLoggingVO.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
			return alarmLoggingVO;
		}).collect(Collectors.toList());
		page.setRecords(collect);
		return R.ok(page );
	}

}
